package com.cjw.campus.core.mybatisplus;

import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cjw.campus.system.entity.ForegroundUserTable;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import java.time.LocalDateTime;

@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        System.out.println(request.getHeader("Authorization"));
        String token = request.getHeader("Authorization");
        if (token == null){
            this.strictInsertFill(metaObject, "createor", () -> "超级管理员", String.class);
            this.strictInsertFill(metaObject, "updater", () -> "超级管理员", String.class);
        } else {
            //  获取token 赋值给jwt
            JWT jwt = JWTUtil.parseToken(token);
            //  解析token里的前台用户信息
            Object object = jwt.getPayload("foregroundUser");
            ForegroundUserTable foregroundUser = JSONUtil.toBean(JSONUtil.toJsonStr(object), ForegroundUserTable.class);
//            System.out.println("==========================");
//            System.out.println(JSONUtil.toJsonStr(object));
            this.strictInsertFill(metaObject, "createor", () -> foregroundUser.getNickName(), String.class);
            this.strictInsertFill(metaObject, "updater", () -> foregroundUser.getNickName(), String.class);
        }
        System.out.println("新增时使用");
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "deleteFlag", () -> false, Boolean.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        if (token == null){
            this.strictUpdateFill(metaObject, "updater", () -> "超级管理员", String.class);
        } else {
            //  获取token 赋值给jwt
            JWT jwt = JWTUtil.parseToken(token);
            //  解析token里的前台用户信息
            Object object = jwt.getPayload("foregroundUser");
            ForegroundUserTable foregroundUser = JSONUtil.toBean(JSONUtil.toJsonStr(object), ForegroundUserTable.class);
            this.strictUpdateFill(metaObject, "updater", () -> foregroundUser.getNickName(), String.class);
        }

        System.out.println("修改时使用");
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
    }
}
